package com.iss.supermarket.UserMangerUI;

import com.iss.supermarket.util.List.ListModelArray;
import com.iss.supermarket.util.entity.SpUser;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class UserManagerList extends JFrame {
    //单例模式
    private static UserManagerList userManagerList;
    //定义组件
    private JList<SpUser> jList;
    private ListModelArray<SpUser> listModelArray;
    private JPanel userManagerListPanel;

    public UserManagerList(){
        InitUI();
    }

    private void InitUI(){
        userManagerListPanel = new JPanel();

        Container container = getContentPane();
        Box verticalBox = Box.createVerticalBox();

        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setPreferredSize(new Dimension(1600,400));

        JPanel HeaderPanel = new JPanel();

        JLabel jLabel1 = new JLabel("用户UUID");
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setPreferredSize(new Dimension(250,15));
        jLabel1.setFont(new Font("黑体",Font.PLAIN,15));
        HeaderPanel.add(jLabel1);

        JLabel jLabel2 = new JLabel("用户名");
        jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel2.setPreferredSize(new Dimension(150,15));
        jLabel2.setFont(new Font("黑体",Font.PLAIN,15));
        HeaderPanel.add(jLabel2);

        JLabel jLabel3 = new JLabel("密码");
        jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel3.setPreferredSize(new Dimension(150,15));
        jLabel3.setFont(new Font("黑体",Font.PLAIN,15));
        HeaderPanel.add(jLabel3);

        JLabel jLabel4 = new JLabel("邮箱");
        jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel4.setPreferredSize(new Dimension(150,15));
        jLabel4.setFont(new Font("黑体",Font.PLAIN,15));
        HeaderPanel.add(jLabel4);

        JLabel jLabel5 = new JLabel("手机号码");
        jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel5.setPreferredSize(new Dimension(150,15));
        jLabel5.setFont(new Font("黑体",Font.PLAIN,15));
        HeaderPanel.add(jLabel5);

        JLabel jLabel6 = new JLabel("真实姓名");
        jLabel6.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel6.setPreferredSize(new Dimension(150,15));
        jLabel6.setFont(new Font("黑体",Font.PLAIN,15));
        HeaderPanel.add(jLabel6);

        JLabel jLabel7 = new JLabel("创建时间");
        jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel7.setPreferredSize(new Dimension(150,15));
        jLabel7.setFont(new Font("黑体",Font.PLAIN,15));
        HeaderPanel.add(jLabel7);

        JLabel jLabel8 = new JLabel("角色");
        jLabel8.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel8.setPreferredSize(new Dimension(150,15));
        jLabel8.setFont(new Font("黑体",Font.PLAIN,15));
        HeaderPanel.add(jLabel8);

        JLabel jLabel9 = new JLabel("是否为锁定状态");
        jLabel9.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel9.setPreferredSize(new Dimension(150,15));
        jLabel9.setFont(new Font("黑体",Font.PLAIN,15));
        HeaderPanel.add(jLabel9);

        jScrollPane.setColumnHeaderView(HeaderPanel);

        listModelArray = new ListModelArray<SpUser>();
        listModelArray.setList(new ArrayList<SpUser>());

        jList = new JList<SpUser>(listModelArray);
        jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        jScrollPane.setViewportView(jList);
        verticalBox.add(jScrollPane);
        userManagerListPanel.add(verticalBox);

//        container.add(userManagerListPanel);
//        setSize(800,800);
//        setVisible(true);
    }

    public void flushList(SpUser spUser){
        if(listModelArray.getSize() <= 0){
            listModelArray.addList(spUser);
        }else if(!isContain(listModelArray,spUser)){
            listModelArray.addList(spUser);
        }

        jList.setCellRenderer(new ListCellRenderer<SpUser>() {
            @Override
            public Component getListCellRendererComponent(JList<? extends SpUser> list, SpUser spuser, int index, boolean isSelected, boolean cellHasFocus) {

                JPanel jPanelTableHeader = new JPanel();
                if(cellHasFocus){
                    jPanelTableHeader.setBackground(Color.RED);
                }
                else if(isSelected){
                    jPanelTableHeader.setBackground(Color.BLUE);
                }else{
                    jPanelTableHeader.setBackground(Color.white);
                }

                JLabel UserUUID = new JLabel(spuser.getUserUuid());
                UserUUID.setHorizontalAlignment(SwingConstants.CENTER);
                UserUUID.setPreferredSize(new Dimension(250,15));
                UserUUID.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(UserUUID);

                JLabel UserName = new JLabel(spuser.getUserName());
                UserName.setHorizontalAlignment(SwingConstants.CENTER);
                UserName.setPreferredSize(new Dimension(150,15));
                UserName.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(UserName);

                JLabel Password = new JLabel(spuser.getPassword());
                Password.setHorizontalAlignment(SwingConstants.CENTER);
                Password.setPreferredSize(new Dimension(150,15));
                Password.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(Password);

                JLabel Email = new JLabel(spuser.getEmail());
                Email.setHorizontalAlignment(SwingConstants.CENTER);
                Email.setPreferredSize(new Dimension(150,15));
                Email.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(Email);

                JLabel Mobile = new JLabel(spuser.getMobile());
                Mobile.setHorizontalAlignment(SwingConstants.CENTER);
                Mobile.setPreferredSize(new Dimension(150,15));
                Mobile.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(Mobile);

                JLabel NiceName = new JLabel(spuser.getNiceName());
                NiceName.setHorizontalAlignment(SwingConstants.CENTER);
                NiceName.setPreferredSize(new Dimension(150,15));
                NiceName.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(NiceName);

                JLabel Modtime = new JLabel(spuser.getModtime());
                Modtime.setHorizontalAlignment(SwingConstants.CENTER);
                Modtime.setPreferredSize(new Dimension(150,15));
                Modtime.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(Modtime);

                JLabel Remark = new JLabel(spuser.getRemark());
                Remark.setHorizontalAlignment(SwingConstants.CENTER);
                Remark.setPreferredSize(new Dimension(150,15));
                Remark.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(Remark);

                JLabel DefFlag = new JLabel();
                //检测用户是否被锁定
                if(spuser.getDelFlag() == 1){
                    //1为锁定，0为没有锁定
                    DefFlag.setText("已锁定");
                    //将字体设置为红色
                    DefFlag.setForeground(Color.red);
                }else{
                    //未锁定则不显示
                    DefFlag.setText("");
                }
                DefFlag.setHorizontalAlignment(SwingConstants.CENTER);
                DefFlag.setPreferredSize(new Dimension(150,15));
                DefFlag.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(DefFlag);

                return jPanelTableHeader;
            }
        });
    }

    public void Clear(){
        ListModelArray listModelArray1 = (ListModelArray)jList.getModel();
        listModelArray1.removeAllElements();
        listModelArray1.clearList();
        jList.repaint();
        jList.validate();
    }

    private boolean isContain(ListModelArray<SpUser> listModelArray,SpUser spUser){
        for(int i=0;i<listModelArray.getSize();++i){
            if(listModelArray.getElementAt(i).getUserUuid().equals(spUser.getUserUuid())){
                return true;
            }
        }
        return false;
    }

    public JPanel getUserManagerListPanel(){
        return userManagerListPanel;
    }

    public JList<SpUser> getjList() {
        return jList;
    }

    public static synchronized UserManagerList getuserManagerList(){
        if(userManagerList == null){
            userManagerList = new UserManagerList();
        }
        return userManagerList;
    }
}
